'''
Created on 29/05/2009

@author: eh01
'''
from yarra.datastore import dsmanager, sqlite3dao
from yarra.handler.parsewsdlhandler import ParseWSDLHandler
from yarra.datastore.daofactory import DAOFactory
import os
import unittest


class ParseWSDLHandlerTest(unittest.TestCase):

    def setUp(self):
        dsmanager.initialize()
#        dst1 = DST(id=1, subject=".", predicate=dsd.PREDICATE_TITLE, object=None, literal="WSDL", literal_type=None, xml_string=None)
#        dst2 = DST(id=2, subject="title", predicate=dsd.PREDICATE_LITERAL_TYPE, object="xsd:string", literal=None, literal_type=None, xml_string=None)
#        dst3 = DST(id=3, subject="wsdl", predicate=dsd.PREDICATE_LITERAL_TYPE, object="xsd:string", literal=None, literal_type=None, xml_string=None)
#        dst4 = DST(id=4, subject="portType/operation", predicate=dsd.PREDICATE_OBJECT_TYPE, object=dsd.OBJECT_LOCALURI, literal=None, literal_type=None, xml_string=None)
#        dst5 = DST(id=5, subject="portType/operation", predicate=dsd.PREDICATE_CARDINALITY, object=dsd.OBJECT_ZERO_OR_MORE, literal=None, literal_type=None, xml_string=None)
#        dst6 = DST(id=6, subject="portType/operation#name", predicate=dsd.PREDICATE_LITERAL_TYPE, object="xsd:string", literal=None, literal_type=None, xml_string=None)
#        dst7 = DST(id=7, subject="portType/operation/input/message/part", predicate=dsd.PREDICATE_OBJECT_TYPE, object=dsd.OBJECT_URI, literal=None, literal_type=None, xml_string=None)
#        dst8 = DST(id=8, subject="portType/operation/input/message/part", predicate=dsd.PREDICATE_CARDINALITY, object=dsd.OBJECT_ZERO_OR_MORE, literal=None, literal_type=None, xml_string=None)
#        dst9 = DST(id=9, subject="portType/operation/output/message/part", predicate=dsd.PREDICATE_OBJECT_TYPE, object=dsd.OBJECT_URI, literal=None, literal_type=None, xml_string=None)
#        dst10 = DST(id=8, subject="portType/operation/output/message/part", predicate=dsd.PREDICATE_CARDINALITY, object=dsd.OBJECT_ZERO_OR_MORE, literal=None, literal_type=None, xml_string=None)
#        dsts = [dst1,
#                dst2,
#                dst3,
#                dst4,
#                dst5,
#                dst6,
#                dst7,
#                dst8,
#                dst9,
#                dst10]
#        dso = DSO(id=1, created_timestamp=None, version=1, active=True, xml_string=None, dsts=dsts)
#        dso.uri = 'http://schemas.xmlsoap.org/wsdl/'

    def tearDown(self):
        os.remove(sqlite3dao.DATABASE_NAME)

    def test_do_WSDL_with_types(self):
        dsodao = DAOFactory().dsodao
        parse_WSDL_handler = ParseWSDLHandler()
        path = os.path.split(__file__)[0]
        dso = parse_WSDL_handler.do(os.path.join(path, "WSDLFile1.wsdl"))
        assert dso.uri == 'http://www.example.org/WSDLFile1/'
        assert len(dso.dsts) == 8
        assert len(dso.get_dsts(subject=".", predicate="operation")) == 2
        dsts = dso.get_dsts(subject=".", predicate="operation")
        # check for wsdl file
        for dst in dsts:
            a_ref = dst.object
            assert len(dso.get_dsts(subject=a_ref)) == 3
            assert len(dso.get_dsts(subject=a_ref, predicate="name")) == 1
            assert len(dso.get_dsts(subject=a_ref, predicate="input")) == 1
            assert len(dso.get_dsts(subject=a_ref, predicate="output")) == 1
            input_uri = dso.get_dsts(subject=a_ref, predicate="input")[0].object
            dsos = dsodao.find_by_URIs([input_uri])
            if len(dsos) == 1:
                dsos[0]
            
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()